package ui.activity.mine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.CheckPhoneNumberUtils;
import Utils.FileUtil;
import Utils.OCRUtils;
import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import model.PersonalCenterBean;
import ui.activity.mine.biz.PersonalCenterBiz;
import ui.activity.mine.component.DaggerPersonalCenterComponent;
import ui.activity.mine.contract.PersonalCenterContract;
import ui.activity.mine.module.PersonalCenterModule;
import ui.activity.mine.presenter.PersonalCenterPresenter;
import ui.model.UploadImageBean;


/**
 * 个人中心
 * Created by chenjian on 2017/8/4.
 */

public class PersonalCenterAct extends BaseAct implements PersonalCenterContract.View {


    @Inject
    PersonalCenterPresenter presenter;
    @Inject
    PersonalCenterBiz biz;
    @BindView(R.id.personalcenter_txt1)
    TextView realName;
    @BindView(R.id.personalcenter_txt2)
    TextView loginAccount;
    @BindView(R.id.personalcenter_txt3)
    TextView personCode;
    @BindView(R.id.personalcenter_txt4)
    TextView iDCard;
    @BindView(R.id.personalcenter_txt5)
    TextView backNumber;
    @BindView(R.id.personalcenter_img)
    SimpleDraweeView personalImg;

    private SharedPreferences sp;

    private static final int REQUEST_CODE_PICK_IMAGE_USER = 201;
    String id;

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_personal_center);
    }

    @Override
    protected void setup() {
        DaggerPersonalCenterComponent.builder().personalCenterModule(new PersonalCenterModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.personalcenter), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        id = sp.getString("id", "0");
        presenter.getUserInfo(id);
        RxView.clicks(personalImg)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> OCRUtils.startUserIcon(this, REQUEST_CODE_PICK_IMAGE_USER));
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 上传头像 第一地址
        if (requestCode == REQUEST_CODE_PICK_IMAGE_USER && resultCode == Activity.RESULT_OK) {
            presenter.toLoadImage(id, FileUtil.getUserIconFile(this).getAbsolutePath());
        }
    }

    @Override
    public void upDateUI(PersonalCenterBean.DataBean bean) {
        personalImg.setImageURI(Uri.parse(bean.getCustom().getHeadImage()));
        realName.setText(bean.getCustom().getRealName());
        loginAccount.setText(CheckPhoneNumberUtils.hideCardNo(bean.getCustom().getCustomName()));
        personCode.setText(bean.getCustom().getRecommendationCode());
        iDCard.setText(CheckPhoneNumberUtils.hideCardNo(bean.getCustom().getIdCardNo()));
        backNumber.setText(CheckPhoneNumberUtils.hideCardNo(bean.getCustom().getBankNo()));
    }

    @Override
    public void upDateImage(UploadImageBean info) {
        personalImg.setImageURI(Uri.parse(info.getHeadImag()));
    }

}
